function bindAddBill() {
	$('#addBill').one('click', function() {
		var param = {};
		param.contractId = contractId;
		jQuery.ajax({
			type : 'get',
			data : param,
			url : basePath + '/hostPayment/add',
			success : function(data) {
				var $dialog = $('#addRentBillHistory_div');
				$dialog.html(data);
				$dialog.dialog({
					title : '新增账单',
					width : 400,
					height : 217,
					closed : false,
					modal : true,
					buttons : [ {
						text : '保存',
						iconCls : 'icon-ok',
						handler : function() {

						}
					} ]
				});
			}
		});
		bindAddBill();
	});
}

function bindAgentChange() {
	$('#openAgentWindow').one('click', function() {
		var param = {};
		param.contractId = contractId;
		param.oldAgentId=agentMaintainBy;
		jQuery.ajax({
			type : 'get',
			data : param,
			url : basePath + '/hostContract/changeAgent',
			success : function(data) {
				var $dialog = $('#changeAgent_div');
				$dialog.html(data);
				$dialog.dialog({
					title : '变更经纪人',
					width : 450,
					height : 217,
					closed : false,
					modal : true,
					buttons : [ {
						text : '保存',
						iconCls : 'icon-ok',
						handler : function() {
							var newAgent = $("#newAgent").val();
							var ifAll = $("#changeAll").is(":checked");
							var option = $("#newAgent").find("option:selected");
							var newName = option.attr("agentName");
							var newMobile = option.attr("mobile");
							var confirmMessage="";
							if(ifAll){
								param.ifAll=1;
								confirmMessage="确定要将\""+agentMaintainByName+" "+agentMaintainByMobile 
								+"\"的当前委托合同的房管经纪人变更为：\""+newName+" "+newMobile+"\"?";
							}else{
								confirmMessage="确定要将\""+agentMaintainByName+" "+agentMaintainByMobile 
								+"\"的"+agentMaintainByCount+"个委托合同的房管经纪人变更为：\""+newName+" "+newMobile+"\"?";
								param.ifAll=0;
							}
							param.newAgentId=newAgent;
							
							$.messager.confirm('确认',confirmMessage,function(r){   
							    if (r){   
							    	jQuery.ajax({
										type : 'post',
										data : param,
										url : basePath + '/hostContract/updateAgent',
										success : function(data) {
											alert("变更成功");
										}
									});
							    }   
							}); 
						}
					}, {
						text : '取消',
						iconCls : 'icon-cancel',
						handler : function() {
							$dialog.dialog('close');
						}
					} ]
				});
			}
		});
		bindAgentChange();
	});
}

function bindOperatorChange() {
	$('#openOperatorWindow').one('click', function() {
		var param = {};
		param.contractId = contractId;
		param.oldAgentId=operateMaintainBy;
		jQuery.ajax({
			type : 'get',
			data : param,
			url : basePath + '/hostContract/changeOperator',
			success : function(data) {
				var $dialog = $('#changeOperator_div');
				$dialog.html(data);
				$dialog.dialog({
					title : '变更运营专员',
					width : 450,
					height : 217,
					closed : false,
					modal : true,
					buttons : [ {
						text : '保存',
						iconCls : 'icon-ok',
						handler : function() {
							var ifAll = $("#changeAllOperator").is(":checked");
							var newOperator = $("#newOperator").val();
							var option = $("#newOperator").find("option:selected");
							var newName = option.attr("agentName");
							var newMobile = option.attr("mobile");
							var confirmMessage="";
							if(ifAll){
								param.ifAll=1;
								confirmMessage="确定要将\""+operateMaintainByName+" "+operateMaintainByMobile 
								+"\"的"+operateMaintainByCount+"个委托合同的房管运营专员变更为：\""+newName+" "+newMobile+"\"?";
							}else{
								param.ifAll=0;
								confirmMessage="确定要将\""+operateMaintainByName+" "+operateMaintainByMobile 
								+"\"的当前委托合同的房管运营专员变更为：\""+newName+" "+newMobile+"\"?";
							}
							param.newAgentId=newAgent;
							$.messager.confirm('确认',confirmMessage,function(r){   
							    if (r){   
							    	jQuery.ajax({
										type : 'post',
										data : param,
										url : basePath + '/hostContract/updateOperator',
										success : function(data) {
											alert("变更成功");
										}
									});
							    }   
							}); 
							alert(newOperator);
						}
					}, {
						text : '取消',
						iconCls : 'icon-cancel',
						handler : function() {
							$dialog.dialog('close');
						}
					} ]
				});
			}
		});
		bindOperatorChange();
	});
}

$(function() {

	$(".subBar").click(function() {
		$(".subBar").removeClass("clicked");
		$(this).addClass("clicked");
		var targetTable = "div " + "." + $(this).attr('data');
		$(".bar-table").hide();
		$(targetTable).show();
	});

	$("#followUp").click(function() {
		var memo = $("#memo").val();
		if (!$.trim(memo)) {
			$.messager.alert('Warning', "跟进内容不能为空！");
			return;
		}
		var param = {};
		param.memo = memo;
		param.contractId = contractId;
		jQuery.ajax({
			type : 'post',
			data : param,
			url : basePath + '/hostContract/updateContractLog',
			success : function(data) {
				console.log(data);
			}
		});

	});

	bindAddBill();
	bindAgentChange();
	bindOperatorChange();

	var checkUrls = [ 'closeContract', 'checkContractInfo','checkContractCertification' ];
	$(".doCheck").click(function() {
		var val = $(this).attr("val");
		var surfix = checkUrls[val];
		alert(surfix);
		var param={};
		param.contractId=contractId;
		jQuery.ajax({
			type : 'post',
			data : param,
			url : basePath + '/hostContract/'+surfix,
			success : function(data) {
				console.log(data);
			}
		});
	});
});